package com.yami.shop.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.WxRecord;
import com.yami.shop.bean.param.WxRecordParam;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.IWxRecordService;
import io.swagger.annotations.ApiOperation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.Date;


/**
 * <p>
 * 用户表单提交记录表 前端控制器
 * </p>
 *
 * @author kubilewang
 * @since 2022-02-27
 */
@Controller
@RequestMapping("/admin/record")
public class WxRecordController {
    @Autowired
    private IWxRecordService iWxRecordService;

    @Autowired
    private MapperFacade mapperFacade;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('record:record:page')")
    public ResponseEntity<IPage<WxRecord>> page(WxRecordParam recordParam, PageParam<WxRecord> page) {
        IPage<WxRecord> orderIPage = iWxRecordService.getList(page, recordParam);
        return ResponseEntity.ok(orderIPage);
    }


    @PostMapping("/addRecord")
    @ApiOperation(value = "新增记录", notes = "新增记录")
    public ResponseEntity<String> addRecord(@Valid @RequestBody WxRecordParam addrParam) {
        String userId = SecurityUtils.getUser().getUserId();
        WxRecord wxRecord = mapperFacade.map(addrParam, WxRecord.class);
        wxRecord.setUserId(userId);
        wxRecord.setCreateTime(new Date());
        wxRecord.setUpdateTime(new Date());
        iWxRecordService.save(wxRecord);
        return ResponseEntity.ok("添加记录成功");
    }

}
